import { _decorator , CCString, Component,warn,Label, RichText, TTFFont} from "cc";
import { EDITOR } from "cc/env";
import { LanguageData } from "./LanguageData";
 
const {ccclass,property,menu} = _decorator;

@ccclass("LangLabelParamsItem")
export class LangLabelParamsItem {
    @property
    key:string  = ""
    @property
    value:string = ""
}


@ccclass("LanguageLabel")
@menu("HiyOOOFramework/Language/LanguageLabel (文本多语言)")
export class LanguageLabel extends Component{
    @property({
        type:LangLabelParamsItem,
        displayName:"params"
    })
    private _params :Array<LangLabelParamsItem>=[];

    @property({
        type:LangLabelParamsItem,
        displayName:"params"
    })
    set  parsms(value:Array<LangLabelParamsItem>){
        this._params = value;
        if (!EDITOR) {
            // this._needUpdate = true;
            this._needUpdate = true;
        }
    }

    get parsms (){
        return this._params || []
    }

    @property({serializable:true})
    private _dataID:string ="";
    @property({type:CCString,serializable:true})
    get dataID(){
        return this._dataID || ""
    }
    set dataID(value:string){
        this._dataID = value
        if (!EDITOR) {
            this._needUpdate = true;
        }
    }

    get string():string{
        let _string = LanguageData.getLangByID(this.dataID);
        if (_string && this._params.length > 0) {
            this._params.forEach((item:LangLabelParamsItem)=>{
                _string = _string.replace(`${item.key}`,item.value);
            })
        }

        if (!_string) {
            warn("[LanguageLabel] 未找到语言标识，使用dataID替换")
            _string = this._dataID;
        }

        return _string;
    }

    /**
     * 更新语言
    */
    language(){
        this._needUpdate = true;
    }

    /**初始字体大小 */
    initFontSize:number = 0;

    /**默认加载需要更新字体  */
    protected onLoad(): void {
        this._needUpdate = true;
    }

    /**
     * 修改多语言参数   
     * TODO: 作者这里采用惰性求值。
     * @param key i18n的key
     * @param value i18n的 文本
     */
    public setVars(key:string,value:string):void{
        let has = false;
        for (let i = 0; i < this._params.length; i++) {
            let element:LangLabelParamsItem = this._params[i]
            if (element.key == key) {
                element.value = value;
                has = true;
            }
        }
        //如果表中没有,则
        if (!has) {
            let paramItem = new LangLabelParamsItem();
            paramItem.key = key;
            paramItem.value = value;
            this._params.push(paramItem)
        }
        this._needUpdate=true;
    }

    protected update(dt: number): void {
        if (this._needUpdate) {
            this._needUpdate = false;
            this.updateContent();
        }
    }

    public updateContent(){
        const label  = this.getComponent(Label);
        const richtext = this.getComponent(RichText);
        const font:TTFFont | null = LanguageData.font;

        if (label) {
            if (font) {
                label.font = font;
            }
            label.string = this.string;
            this.initFontSize = label.fontSize;
        }else if(richtext){
            if(font){
                richtext.font = font;
            }
            richtext.string = this.string;
            this.initFontSize = label.fontSize;
        }else{
            warn("[LanguageLabel], 该节点没有cc.Label || cc.RichText组件")
        }
    }

    /**
     *  TODO:核查
     * 
    */
    private _needUpdate = false;

}